<!DOCTYPE html>
<html>
<head>
<title>Potential Barrier Diagram</title>
</head>
<body>
<canvas id="physicsCanvas" width="600" height="420"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // Style settings
    ctx.fillStyle = 'black';
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 2.5;
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    // Coordinates
    const originX = 80;
    const originY = 330;
    const axisLenX = 470;
    const axisLenY = 280;

    // Helper function for drawing arrows on axes
    function drawAxisArrow(ctx, fromx, fromy, tox, toy) {
        const headlen = 12; // length of head in pixels
        const dx = tox - fromx;
        const dy = toy - fromy;
        const angle = Math.atan2(dy, dx);
        ctx.moveTo(fromx, fromy);
        ctx.lineTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
        ctx.moveTo(tox, toy);
        ctx.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
    }
    
    // Helper function for drawing V0 label with subscript
    function drawV0(ctx, x, y) {
        ctx.font = 'italic 24px serif';
        ctx.fillText('V', x, y);
        ctx.font = 'italic 18px serif';
        ctx.fillText('0', x + 12, y + 6);
    }

    // Draw Axes
    ctx.beginPath();
    drawAxisArrow(ctx, originX, originY, originX + axisLenX, originY); // x-axis
    drawAxisArrow(ctx, originX, originY, originX, originY - axisLenY); // y-axis
    ctx.stroke();

    // Axes Labels
    ctx.font = 'italic 24px serif';
    ctx.fillText('x', originX + axisLenX + 15, originY);
    ctx.fillText('V(x)', originX, originY - axisLenY - 20);

    // Barrier parameters
    const barrierStartX = originX + 180;
    const barrierWidth = 150;
    const barrierEndX = barrierStartX + barrierWidth;
    const v0Height = 200;
    const v0_y = originY - v0Height;
    // According to the problem, E=1eV and V0=2eV, so E is at half the height of V0
    const eHeight = v0Height / 2; 
    const e_y = originY - eHeight;

    // Draw Potential Barrier Shape
    ctx.beginPath();
    ctx.moveTo(originX - 10, originY); // Start slightly left of the y-axis
    ctx.lineTo(barrierStartX, originY);
    ctx.lineTo(barrierStartX, v0_y);
    ctx.lineTo(barrierEndX, v0_y);
    ctx.lineTo(barrierEndX, originY);
    ctx.lineTo(originX + axisLenX, originY);
    ctx.stroke();

    // Draw Energy Levels and labels
    // V0
    ctx.beginPath();
    ctx.setLineDash([5, 5]);
    ctx.moveTo(originX, v0_y);
    ctx.lineTo(barrierStartX, v0_y);
    ctx.stroke();
    drawV0(ctx, originX - 45, v0_y);
    
    // E
    ctx.beginPath();
    ctx.moveTo(originX, e_y);
    ctx.lineTo(originX + 100, e_y);
    ctx.stroke();
    
    ctx.setLineDash([]); // Reset to solid line
    ctx.beginPath();
    ctx.moveTo(originX + 100, e_y);
    ctx.lineTo(barrierStartX, e_y);
    ctx.stroke();

    ctx.font = 'italic 24px serif';
    ctx.fillText('E', originX - 30, e_y);
    ctx.font = 'italic 20px serif';
    ctx.fillText('a', barrierStartX - 18, e_y);


    // Draw width annotation 'd'
    const d_line_y = v0_y - 30;
    ctx.beginPath();
    // Horizontal line
    ctx.moveTo(barrierStartX, d_line_y);
    ctx.lineTo(barrierEndX, d_line_y);
    // Left arrow head (points left)
    ctx.moveTo(barrierStartX + 10, d_line_y - 6);
    ctx.lineTo(barrierStartX, d_line_y);
    ctx.lineTo(barrierStartX + 10, d_line_y + 6);
    // Right arrow head (points right)
    ctx.moveTo(barrierEndX - 10, d_line_y - 6);
    ctx.lineTo(barrierEndX, d_line_y);
    ctx.lineTo(barrierEndX - 10, d_line_y + 6);
    ctx.stroke();
    ctx.font = 'italic 22px serif';
    ctx.fillText('d', barrierStartX + barrierWidth / 2, d_line_y - 15);
    
    // Draw Figure Caption
    ctx.font = '18px serif';
    ctx.fillText('Fig. 1.22', canvas.width / 2, originY + 60);

</script>
</body>
</html>